#!/bin/bash

# 功能描述：用於配置構建CLI
rootDir=`dirname $(readlink -f "$0")`; headColor='\033[38;2;41;184;219m'; endColor='\033[0m'
cd $rootDir; echo cd `pwd`

#      ____     ____    ____           ______    ____     ___       ____     __       ______
#     / __ \   /  _/   / __ \         / ____/   / __ \   /   |     / __ \   / /      / ____/
#    / /_/_/   / /    / /_/ /        / / __    / /_/ /  / /| |    / / / /  / /      / __/   
#   / /_/\   _/ /    / ____/        / /_/ /   / _, _/  / ___ |   / /_/ /  / /___   / /___   
#  /_____/  /___/   /_/             \____/   /_/ |_|  /_/  |_|  /_____/  /_____/  /_____/  

# CHECK FRIST
echo -e "${headColor}▶ 开始檢查是否更新了：ocean-git.sh ${endColor}"

grep -w "⏳修改后，👉請刪除本行提示🔴" ./ocean-git.sh && gitUpdated="none" || gitUpdated="updated"
if [ "$gitUpdated" == "none" ]
then
	echo '🔴 请先修改项目git地址[ocean-git.sh]'
    code ./ocean-git.sh
	exit 2
fi



# 零、配置JDK
## 检查JDK环境变量
if [ ! -d "$JDK17" ]; then
	echo -e "${headColor}☯ 环境变量【JDK17】不存在，请确认后重启vscode，刷新环境变量缓存 ${endColor}"
    exit 4
fi
if [ ! -d "$JDK25" ]; then
	echo -e "${headColor}☯ 环境变量【JDK17】不存在，请确认后重启vscode，刷新环境变量缓存 ${endColor}"
    exit 4
fi


## 确保JDK软链接存在
lib64_jvm="/usr/lib64/jvm"
if [ ! -d $lib64_jvm ]; then
    echo -e "${headColor}☯ 正在创建【$lib64_jvm】，请输入用户密码 ${endColor}"
    sudo mkdir -p $lib64_jvm
    if [ $? != 0 ]
	then 
		exit 6
	fi
fi


## jdk17软链接
lib64_jdk17="$lib64_jvm/jdk17"
if [ ! -h "$lib64_jdk17" ]; then
    echo -e "${headColor}☯ 正在创建软链接【$lib64_jdk17】 ${endColor}"
    sudo ln -s $JDK17 $lib64_jdk17
elif [ ! -e "$lib64_jdk17" ]; then
    echo -e "${headColor}☯ 软链接已失效，正在重建软链接【$lib64_jdk17】 ${endColor}"
    sudo rm $lib64_jdk17
    sudo ln -s $JDK17 $lib64_jdk17
fi
    if [ $? != 0 ]
	then 
		exit 8
	fi

## jdk25软链接
lib64_jdk25="$lib64_jvm/jdk25"
if [ ! -h "$lib64_jdk25" ]; then
    echo -e "${headColor}☯ 正在创建软链接【$lib64_jdk25】 ${endColor}"
    sudo ln -s $jdk25 $lib64_jdk25
elif [ ! -e "$lib64_jdk25" ]; then
    echo -e "${headColor}☯ 软链接已失效，正在重建软链接【$lib64_jdk25】 ${endColor}"
    sudo rm $lib64_jdk25
    sudo ln -s $jdk25 $lib64_jdk25
fi
    if [ $? != 0 ]
	then 
		exit 9
	fi



# sed兼容mac
sedPrefix="sed -i "
if [ "$(uname)" == "Darwin" ]; then
	echo "This is a Mac."
	sedPrefix="sed -i '' "
fi


## 打开启动检测JVM
$sedPrefix 's!^.*org\.gradle\.java\.installations\.auto-detect.*!org.gradle.java.installations.auto-detect=true!' $rootDir/gradle.properties
## $sedPrefix 's!services\.gradle\.org/distributions!mirrors.cloud.tencent.com/gradle!' $rootDir/gradle/wrapper/gradle-wrapper.properties




# 一、配置HOME
echo -n -e "${headColor}▶ 请输入HOME目录: ${endColor}"
while read homeDir
do
## 如果文件夹不存在
if [ ! -d "$homeDir/modules" ]
then
	echo -e "${headColor} ➩ ${endColor}HOME目录不正确！请重新输入: "
else
	echo -e "${headColor} ➩ ${endColor}更新.vscode/settings.json ..."
    $sedPrefix 's!^.*FIELD_NC_HOME.*!    "FIELD_NC_HOME": "'$homeDir'",!' ./.vscode/settings.json

    ## 添加执行权限
    chmod -R +x $homeDir/ant $homeDir/bin $homeDir/intelliv

    ## 获取当前的HOME
    currentHome=`awk '{ if (index($0,"FIELD_NC_HOME")) { print $0}}' ./.vscode/settings.json | awk -F\" '{print $4}'`
    echo  -e "${headColor} ➩ ${endColor}当前HOME目录："$currentHome
    break
fi
done



# 二、是否拉取项目git仓库
echo -n -e "${headColor}▶ 是否需要拉取項目倉庫【ocean-git.sh】? (Y/n) ${endColor}"
read gitclone

if [ "$gitclone" == "Y" ] || [ "$gitclone" == "y" ]
then
    ## 拉取项目
    echo  -e "${headColor} ➩ ${endColor}开始拉取项目 ..."

    ## 授权
    chmod +x *.sh

    ## 切换到子模块目录
    cd $rootDir/subprojects
    echo `pwd`

    ## 拉取 git repository
    source $rootDir/ocean-git.sh

    ## error exit
	if [ $? != 0 ]
	then 
		exit 2
	fi
else
	echo  -e "${headColor} ➩ ${endColor}跳过拉取項目倉庫"
fi



# 回到根目录
cd $rootDir; echo cd `pwd`


# 三、自动扫描模块依赖
## 授权
chmod +x gradle*
chmod +x *.sh
chmod +x arthas/*


## Refresh Gradle Projects View
echo -e "${headColor}▶ 开始刷新Gradle项目 ... ${endColor}"
# ./gradlew clean -U
./gradlew clean

## 检查任务完整依赖图
echo -e "${headColor}▶ 检查任务完整依赖图 ... ${endColor}"
./gradlew 'init All Dependencies' -m

## 执行初始化模块任务
echo -e "${headColor}▶ 执行初始化模块任务 ... ${endColor}"
./gradlew 'init All Dependencies'



# 四、清理
rm -f ./*.log



# 结束
greenColor='\033[38;2;183;212;7m'
echo ""
sleep 0.2
echo -e "${greenColor}🟢 配置结束；请执行如下命令：${endColor}"
echo -e "${greenColor} ★ ${endColor}F1 -> Java: Clean Java Language Server Workspace"


sleep 0.2
echo -e "${greenColor}🟢 编译结束；请执行如下命令：${endColor}"
## `Terminal` -> `Run Task...` -> `initLaunch`
echo -e "${greenColor} ★ ${endColor}ROOT -> Tasks -> init -> initLaunch"
## `Terminal` -> `Run Task...` -> `syncLocalConfig`
echo -e "${greenColor} ☆ ${endColor}ROOT -> Tasks -> sync -> syncLocalConfig"
   

exit 0